package com.gok.seasonmovie.mapper;

import com.gok.seasonmovie.entity.Comment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CommentMapper {

    @Select("SELECT * FROM comments ORDER BY date DESC")
    List<Comment> getAllComments();

    @Update("UPDATE comments  \n" +
            "SET liked = 1 - liked,  \n" +
            "    likeCount = CASE WHEN liked = 1 THEN likeCount + 1 ELSE likeCount - 1 END  \n" +
            "WHERE id = #{id};")
    void incrementLikeCount(@Param("id") String id);

    @Insert({
            "INSERT INTO comments (",
            "avatarUrl, username, comment, date, location, likeCount , liked",
            ") VALUES (",
            "#{avatarUrl}, #{username}, #{comment}, #{date}, #{location}, 0 , 0",
            ")"
    })
    void insertComment(String avatarUrl,String username,String comment,String date,String location);
}
